<?php
namespace app\wxapp\controller;

use app\model\Ad; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Order; 
use app\model\Userdata; 

class Wkpay extends Base
{
    public function Wkpay()
    {/** * Desc: * Author: wp * Time: 2022/5/27 6:00 下午 * Email: 1565602678@qq.com */global $_G, $_P;$fans = $this->checkuser();$system = $_G['system'];$themeid = intval($_P['themeid']);$theme = Theme::getThemeByIdxcx($themeid);if (empty($theme['id'])) {    wxapp_result(1, '活动入口不正确');    exit;}$fromtype = $_P['from'];if ($fromtype!='wxapp'){    return_json(1,'非法请求');    exit;}$config = Theme::getConfig($themeid);$gift_id = intval($_P['gift_id']);$wk_id = intval($_P['wk_id']);//未支付的进行较验是否已支付Order::checkorder($themeid, $config, $system);//Userdata::addLog($themeid, $_G['member']['uid'], 6);if ($theme['status'] == 0) {    wxapp_result(1, '活动未开始');    exit;}if ($theme['status'] == 2) {    wxapp_result(1, '活动已结束');    exit;}//Ad::saveMBXX(); //保存模版消息$myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);if (!empty($myworker) && $myworker['level']<=3 && !$config['wk_worker_gift_type']){    wxapp_result(1, '员工不允许打赏礼物！');    exit;}$wkInfo = db_find("select * from ". tablename(wk_info) ." where id=$wk_id and themeid=$themeid");$wk_config = json_decode($wkInfo['config'],true);$wk_gift_group_id = $wk_config['wk_gift_group_id'];$fee = 0;//查找礼物信息，包含金额$sql = "SELECT * FROM " . tablename(wk_gift) . " WHERE id = $gift_id and group_id=$wk_gift_group_id";$giftInfo = db_find($sql);if (empty($giftInfo)){    wxapp_result(1, '未找到礼物信息！');    exit;}$fee = floatval($giftInfo['price']);//查找分组信息的themeid是否与传参匹配$sql1 = "SELECT * FROM " . tablename(wk_gift_group) . " WHERE id = ".$wk_gift_group_id;$row1 = db_find($sql1);if (empty($row1)){    wxapp_result(1, '未找到礼物分组信息！');    exit;}if (intval($row1['themeid']) != $themeid){    wxapp_result(1, '活动信息与礼物分组信息不匹配！');    exit;}if ($fee <= 0) {    wxapp_result(1, '打赏礼物金额不可为0元');    exit;}//写入赠送礼物记录表//查找礼物信息$uid = $_G['member']['uid'];if ($uid == $wkInfo['uid']){    wxapp_result(1, '不可以给自己打赏礼物');    exit;}$record_data = [    'themeid'=>$themeid,    'wk_id'=>$wk_id,    'from_uid'=>$uid,    'to_uid'=>$wkInfo['uid'],    'gift_id'=>$gift_id,    'price'=>$fee,    'status'=>0,    'pay_info'=>json_encode(array()),    'created_at'=>date("Y-m-d H:i:s"),    'updated_at'=>date("Y-m-d H:i:s"),];//存入数据库$record_res = $insertid = db_insert(wk_give_gift_record,$record_data);if (empty($record_res)){    wxapp_result(1, '打赏记录失败！');    exit;}$record_id = $insertid;if (!$record_id){    wxapp_result(1, '未找到记录信息！');    exit;}//组装数组$data = array(    'uniacid' => $_G['mpid'],    'ordertype' => 100,//暂定义为类型100为打赏礼物    'themeid' => $themeid,    'openid' => $_G['openid'],    'uid' => $uid,    'username' => $fans['username'],    'mobile' => $fans['mobile'],    'status' => 0,    'type' => 'wxapp',);$data['orderfee'] = $fee;$data['masterid'] = $wk_id;//主id为微客id$data['detailid'] = $record_id;//详细id为记录id$orderid = 0;$tid = '';$module_item = db_get('yx_sys_modules', ['module_name' => MODULE_NAME, 'themeid' => $themeid]);
        $module_id = empty($module_item['id']) ? '000000' : sprintf("%06d", $module_item['id']);
        $uniontid = date('YmdHis') . $module_id . random(8, 1);
        $billcode = getMillisecond();$tid = '0' . $billcode;
            $data['uniontid'] = $uniontid;$data['tid'] = $tid;$data['addtime'] = TIMESTAMP;$data['ordersn'] = $billcode;$orderid = Order::save($data);$ordertype = 'theme';if (empty($_G['openid']) || empty($_G['member']['uid'])) {    wxapp_result(1, '用户信息错误');    return;}$order = Order::getByIdAndOpenid($orderid, $_G['openid']);if (empty($order)) {    wxapp_result(1, '记录不存在，请重新下单');    exit;}if ($order['status'] != 0) {    wxapp_result(1, '定单已完成支付');    exit;}if ($order['orderfee'] <= 0) {    wxapp_result(1, '金额必须大于0元');    exit;}//调用微信支付$desclen = 26;$theme = Theme::getThemeById($order['themeid']);if (!empty($theme['mobile'])) {    $desc = "【客服" . $theme['mobile'] . "】";    $desclen = $desclen - mb_strlen($desc);}if (!empty($theme['jubf'])) {    $desc = mb_substr($theme['jubf'], 0, $desclen, "UTF-8") . $desc;} else {    $desc = mb_substr($theme['title'], 0, $desclen, "UTF-8") . $desc;}$title = $desc;$params = array('isrecharge' => false);//不是分账模式//if ($system['send_mode'] == 1) {//    $params ['profit_sharing'] = 'Y';//}$params ['themeid'] = $order['themeid'];
        $params ['uniontid'] = $order['uniontid'];
        $params ['tid'] = $order['tid'];$params ['user'] = $_G['openid'];$params ['fee'] = $order['orderfee'];$params ['title'] = $title;$params ['ordersn'] = $order['ordersn'];$params ['virtual'] = false;$pay_params = $this->pay($params);if (is_error($pay_params)) {    return wxapp_result(1, '支付失败，请重试', $pay_params);}return wxapp_result(0, '', $pay_params);
}
}